<template>
  <div class="page-next">
    <Row type="flex" justify="center">
      <Col span="16" v-if="!isVerify">
        <Row type="flex" justify="center">
          <Col span="16">
            <Form
              ref="FromRef"
              :model="fromData"
              :rules="fromRule"
              label-position="top"
              @on-validate="formValidate"
            >
              <FormItem label="密码" prop="pwd">
                <Input
                  v-model="fromData.pwd"
                  placeholder="请输入内容"
                  type="password"
                  clearable
                  @input="changeChange()"
                ></Input>
                <div class="pwd-box">
                  <div class="pwd-box-item">
                    <span
                      v-for="(item, index) in 3"
                      :key="item"
                      :class="
                        verifyPassword > index
                          ? 'pwd-box-current' + verifyPassword
                          : ''
                      "
                    ></span>
                  </div>
                  <div class="pwd-box-tips">
                    密码强度：<span>{{ verifyLevel[verifyPassword] }}</span>
                  </div>
                </div>
              </FormItem>
              <FormItem label="确认密码" prop="pwdAgain">
                <Input
                  v-model="fromData.pwdAgain"
                  placeholder="请输入内容"
                  type="password"
                  clearable
                ></Input>
              </FormItem>
            </Form>
            <div class="next-btn">
              <Button
                type="primary"
                :disabled="isBtnDisabled"
                :loading="isLoading"
                @click="handleAgree"
                >确认</Button
              >
            </div>
          </Col>
        </Row>
      </Col>
      <Col span="16" v-else>
        <RegistrFinish
          tipsIcon="ios-checkmark-circle-outline"
          tipsName="设置密码成功"
          tipsContent="您已设置新密码"
          tipsBtnText="去登录"
          tipsBtnType="1"
        />
      </Col>
    </Row>
  </div>
</template>
<script type="text/javascript">
import { ForgotPassword } from "@/api/account.js";
import { encrypt } from "@/utils/encrypFile";

export default {
  props: {
    types: {
      type: Number,
      default: 2,
    },
  },
  data() {
    const validatePwd = (rule, value, callback) => {
      if (this.verifyPassword < 2) {
        callback("密码格式不正确");
      } else {
        callback();
      }
    };

    const validatePwdAgain = (rule, value, callback) => {
      if (this.fromData.pwd != this.fromData.pwdAgain) {
        callback("两次密码不一致");
      } else {
        callback();
      }
    };

    return {
      isBtnDisabled: true,
      fromData: {
        pwd: "",
        pwdAgain: "",
      },
      fromRule: {
        pwd: [
          { required: true, message: "请输入密码", trigger: "change" },
          { validator: validatePwd, trigger: "change" },
        ],
        pwdAgain: [
          { required: true, message: "请再次输入密码", trigger: "change" },
          { validator: validatePwdAgain, trigger: "change" },
        ],
      },
      verifyPassword: 1,
      verifyLevel: {
        1: "低",
        2: "中",
        3: "高",
      },
      isVerify: false,
      isLoading: false,
    };
  },
  methods: {
    async handleAgree() {
      let mobile = localStorage.getItem("updatePwdPhoneNum");
      let { pwd } = this.fromData;
      let params = {
        mobile,
        pwd: encrypt(pwd),
      };
      this.isLoading = true;
      try {
        let data = await ForgotPassword(params);
        this.isLoading = false;
        this.isVerify = true;
      } catch (error) {
        this.isLoading = false;
      }
    },
    // 验证密码
    changeChange() {
      let val = this.fromData.pwd;
      let passwordLen = this.fromData.pwd.length;
      // 包含数字
      let numTest = /(?=.*\d)/.test(val);
      // 包含小写字母
      let aTest = /[a-z]/.test(val);
      // 包含大写字母
      let ATest = /[A-Z]/.test(val);
      // 包含特殊符号
      let AaTest = /[^a-zA-Z\s\d]/.test(val);

      let result = [];
      result.push(numTest, aTest, ATest, AaTest);
      let res = result.filter((o) => o);
      let resLen = res.length;
      if (passwordLen < 8 || resLen <= 2) {
        return (this.verifyPassword = 1);
      }
      if (passwordLen >= 8 && passwordLen < 16 && resLen == 3 && !AaTest) {
        this.verifyPassword = 2;
      } else {
        this.verifyPassword = 3;
      }
    },
    formValidate(prop, status, error) {
      console.log(prop, status, error, "prop, status, error");
      let isValidate = false;
      if (prop == "pwd") {
        console.log(prop, "1111");
        this.$refs.FromRef.validateField("pwdAgain");
      }
      for (let item in this.fromData) {
        if (!this.fromData[item]) {
          isValidate = true;
          break;
        }
      }
      this.isBtnDisabled = isValidate;
    },
  },
  mounted() {},
};
</script>

<style lang="less" scoped>
.next-btn {
  display: flex;
  justify-content: center;
}
.ivu-btn {
  width: 100%;
}
.pwd-box {
  flex: none;
  display: flex;
  align-items: center;
  padding-top: 10px;
  .pwd-box-tips {
    font-size: 12px;
    color: #aaa;
    line-height: 1;
    margin-left: 10px;
  }
}
.pwd-box-item {
  display: flex;
  span {
    display: block;
    width: 60px;
    height: 4px;
    background: #c4c4c4;
    margin-right: 5px;
  }
  span.pwd-box-current1 {
    background: #ff0000;
  }

  span.pwd-box-current2 {
    background: #ffbf6b;
  }

  span.pwd-box-current3 {
    background: #a2ef4d;
  }
}
</style>
